<template>
  <div>
    <div style="margin: 10px 0">
      <el-input
          style="width: 200px"
          placeholder="请输入资产编号"
          suffix-icon="el-icon-search"
          v-model="assetNumber"
      ></el-input>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
      <el-button
          v-if="userType === '00'"
          type="primary"
          @click="exp"
          class="ml-5"
      >导出 <i class="el-icon-download"></i
      ></el-button>
    </div>
    <el-table :data="tableData" style="width: 100%; height: 100%" border>
      <el-table-column fixed prop="lendId" label="借记编号" width="80">
      </el-table-column>
      <el-table-column
          fixed
          prop="lendSerialnumber"
          label="借出编号"
          width="150"
      >
      </el-table-column>
      <el-table-column prop="assetNumber" label="资产编号" width="150">
      </el-table-column>
      <el-table-column prop="name" label="设备名字" width="120">
      </el-table-column>
      <el-table-column prop="type" label="设备类型" width="120">
      </el-table-column>
      <el-table-column prop="borrower" label="借用人" width="120">
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" width="120">
      </el-table-column>
      <el-table-column prop="reason" label="申请原因" width="120">
      </el-table-column>
      <el-table-column prop="addtime" label="申请时间" width="150">
        <template slot-scope="scope">
          {{ formatDate(scope.row.addtime) }}
        </template>
      </el-table-column>
      <el-table-column label="是否借出" align="center">
        <template slot-scope="scope">
          <el-popconfirm
              class="ml-5"
              confirm-button-text="确定"
              cancel-button-text="等一下"
              icon="el-icon-info"
              icon-color="red"
              title="您确定要同意借出这件资产吗？"
              @confirm="handleEdit(scope.row)"
          >
            <el-button type="success" slot="reference"
            >通过 <i class="el-icon-remove-outline"></i
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>


<script>
import {LocalHost} from "@/utils/tools";

export default {
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      assetNumber: "",
      form: {},
    };
  },
  created() {
    this.load();
  },
  methods: {
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    formatDate(dateString) {
      const dateObj = new Date(dateString);
      const year = dateObj.getFullYear();
      const month = String(dateObj.getMonth() + 1).padStart(2, "0");
      const day = String(dateObj.getDate()).padStart(2, "0");
      return `${year}-${month}-${day}`;
    },
    load() {
      this.request
          .get("/lend/outpage", {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              assetNumber: this.assetNumber,
            },
          })
          .then((res) => {
            if (res.success) {
              this.tableData = res.data.records;
              this.total = res.data.total;
            }
          });
    },
    exp() {
      window.open(LocalHost + "/lend/export");
    },
    reset() {
      this.assetNumber = "";
      this.load();
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.load();
    },
    handleEdit(row) {
      row.audit = "是";
      this.request.post("/lend/audit", row).then((res) => {
        if (res.data) {
          // console.log(res.data)
          this.$message.success("同意借出");
          this.dialogFormVisible = false;
          this.load();
        } else {
          this.$message.error("借出失败");
        }
      });
    },
  },
};
</script>